package com.company.huisuo.app

import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import com.company.huisuo.BuildConfig
import com.company.huisuo.repository.AuthRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch

class MyApp : Application() {

    companion object {
        @SuppressLint("StaticFieldLeak")
        lateinit var context: Context

        var isTablet = true

        val isDebug = BuildConfig.DEBUG

        val appScope: CoroutineScope = MainScope()
    }

    override fun onCreate() {
        super.onCreate()
        context = this
        isTablet = context.resources.configuration.smallestScreenWidthDp >= 600
        
        // 初始化时自动登录
        autoLogin()
    }
    
    /**
     * 自动登录
     */
    private fun autoLogin() {
        appScope.launch {
            try {
                val authRepository = AuthRepository()
                if (!authRepository.isLoggedIn()) {
                    // 如果未登录，执行设备登录
                    val result = authRepository.deviceLogin()
                    if (result.isSuccess) {
                        println("自动登录成功")
                    } else {
                        println("自动登录失败: ${result.exceptionOrNull()?.message}")
                    }
                } else {
                    println("用户已登录")
                }
            } catch (e: Exception) {
                println("自动登录异常: ${e.message}")
            }
        }
    }

}